package com.pashanhoo.netty.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 胡庆安
 * @version V1.0
 * @Package com.pashanhoo.netty.bio
 * @date 2020/11/4 15:41
 * @Copyright © 2010-2020 爬山虎科技（扬州）股份有限公司
 */
public class BioServer {
    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        //创建serverSocket
        ServerSocket serverSocket = new ServerSocket(6666);
        System.out.println("服务器启动了");

        final Socket accept = serverSocket.accept();
        System.out.println("连接到一个客户端");

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                handle(accept);
            }
        });


    }

    static void handle(Socket socket) {
        try {
            byte[] bytes = new byte[1024];
            InputStream inputStream = socket.getInputStream();
            while (true) {
                int read = inputStream.read();
                if (read!=-1){
                    System.out.println(new String(bytes,0,read));
                }else {
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
